import 'dart:io';
import 'package:uuid/uuid.dart';
import '../../data/models/upload/upload_task.dart';
import '../../utils/file_utils.dart';

/// 上传任务工厂类
class UploadTaskFactory {
  static const _uuid = Uuid();
  static const int _smallFileThreshold = 20 * 1024 * 1024; // 20MB

  /// 创建单个上传任务
  UploadTask createUploadTask(File file, int directoryId) {
    return UploadTask(
      id: _uuid.v4(),
      filePath: file.path,
      fileName: file.path.split(Platform.pathSeparator).last,
      fileSize: file.lengthSync(),
      directoryId: directoryId,
      type:
          file.lengthSync() > _smallFileThreshold
              ? UploadTaskType.chunkFile
              : UploadTaskType.smallFile,
      createTime: DateTime.now(),
    );
  }

  /// 批量创建上传任务
  List<UploadTask> createUploadTasks(List<File> files, int directoryId) {
    return files.map((file) => createUploadTask(file, directoryId)).toList();
  }

  /// 从选择的文件创建上传任务
  Future<List<UploadTask>> createUploadTasksFromPicker({
    required int directoryId,
    List<String>? allowedExtensions,
  }) async {
    final files = await FileUtils.pickFiles(
      allowedExtensions: allowedExtensions,
    );

    if (files.isEmpty) {
      return [];
    }

    return createUploadTasks(files, directoryId);
  }
}
